<?php

class UserProfilesController extends Controller
{
	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return CMap::mergeArray(parent::filters(),array(
			'accessControl', // perform access control for CRUD operations
		));
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}	

	/**
	 * Displays a particular model.
	 */
	public function actionIndex(){
	   $model=new UserProfiles;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['UserProfiles']))
		{
			$model->attributes=$_POST['UserProfiles'];
			$model->avatar = CUploadedFile::getInstance($model, 'avatar');
			if($model->save()) {
				$path = Yii::getPathOfAlias('webroot').'/data/avatars/';
                $model->avatar->saveAs($path.$model->avatar);
				$this->redirect(array('view','id'=>$model->id));
			}
		}
		$this->render('index',array(
			'model'=>$model,
		));
	}
    public function actionView($id){
        $model=UserProfiles::model()->findByPk((int)$id);
        $this->render('view',array(
			'model'=>$model,
		));
    }
}
